/*
 * Tint Browser for Android
 * 
 * Copyright (C) 2012 - to infinity and beyond J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.cnbrowser.android.ui.managers;

import com.cnbrowser.android.R;
import com.cnbrowser.android.ui.activities.TintBrowserActivity;
import com.cnbrowser.android.utils.Constants;

import android.content.Context;
import android.preference.PreferenceManager;

public class UIFactory {

    public enum UIType {
        TABLET,
        PHONE,
        LEGACY_PHONE,
        CHINA_BROWSER
    }

    private static boolean isInitialized = false;

    private static UIType sUIType;

    public static UIType getUIType(Context context) {
        checkInit(context);
        return sUIType;
    }

    public static boolean isTablet(Context context) {
        checkInit(context);
        return sUIType == UIType.TABLET;
    }

    public static int getMainLayout(Context context) {
        checkInit(context);

        switch (sUIType) {
            case TABLET:
                return R.layout.tablet_main_activity;

            case PHONE:
                return R.layout.phone_main_activity;

            case LEGACY_PHONE:
                return R.layout.legacy_phone_main_activity;

            case CHINA_BROWSER:
                return R.layout.china_browser_main_activity;

            default:
                return R.layout.phone_main_activity;
        }
    }

    public static int getMainMenuLayout(Context context) {
        checkInit(context);

        switch (sUIType) {
            case TABLET:
                return R.menu.main_activity_menu_tablet;

            case PHONE:
            case LEGACY_PHONE:
            case CHINA_BROWSER:
                return R.menu.main_activity_menu;

            default:
                return R.menu.main_activity_menu;
        }
    }

    public static UIManager createUIManager(TintBrowserActivity activity) {
        checkInit(activity);

        switch (sUIType) {
            case TABLET:
                return new TabletUIManager(activity);

            case PHONE:
                return new PhoneUIManager(activity);

            case LEGACY_PHONE:
                return new LegacyPhoneUIManager(activity);

            case CHINA_BROWSER:
                return new ChinaBrowserUIManager(activity);

            default:
                return new PhoneUIManager(activity);
        }
    }

    private static void init(Context context) {
        String uiTypePref = PreferenceManager.getDefaultSharedPreferences(context).getString(Constants.PREFERENCE_UI_TYPE, "LEGACY_PHONE");

        if ("TABLET".equals(uiTypePref)) {
            sUIType = UIType.TABLET;
        } else if ("LEGACY_PHONE".equals(uiTypePref)) {
            sUIType = UIType.LEGACY_PHONE;
        } else if ("CHINA_BROWSER".equals(uiTypePref)) {
            sUIType = UIType.CHINA_BROWSER;
        } else {
            sUIType = UIType.PHONE;
        }

        isInitialized = true;
    }

    private static void checkInit(Context context) {
        if (!isInitialized) {
            init(context);
        }
    }

}
